package com.itcam.lease.web.app.service.impl;

import com.itcam.lease.common.context.LoginUserContext;
import com.itcam.lease.model.entity.*;
import com.itcam.lease.model.enums.ItemType;
import com.itcam.lease.model.enums.LeaseStatus;
import com.itcam.lease.web.app.mapper.*;
import com.itcam.lease.web.app.service.BrowsingHistoryService;
import com.itcam.lease.web.app.service.RoomInfoService;
import com.itcam.lease.web.app.vo.apartment.ApartmentItemVo;
import com.itcam.lease.web.app.vo.attr.AttrValueVo;
import com.itcam.lease.web.app.vo.fee.FeeValueVo;
import com.itcam.lease.web.app.vo.graph.GraphVo;
import com.itcam.lease.web.app.vo.room.RoomDetailVo;
import com.itcam.lease.web.app.vo.room.RoomItemVo;
import com.itcam.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;


    /**
     * 分页查询房间列表
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page, queryVo);
    }

    /**
     * 根据id获取房间的详细信息
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        // 1.查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if (roomInfo == null) {
            return null;
        }
        // 2.查询图片
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);
        // 3.查询租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
        // 4.查询配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
        // 5.查询标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
        // 6.查询支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
        // 7.查询基本属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);
        // 8.查询杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(roomInfo.getApartmentId());
        // 9.查询公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoMapper.selectApartmentItemVoById(roomInfo.getApartmentId());

        // 创建一个新的 RoomDetailVo 对象，用于封装房间的详细信息
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        // 使用 Spring 的 BeanUtils 工具类将 RoomInfo 对象的属性复制到 RoomDetailVo 对象中
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        // 设置公寓信息
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        // 设置图片信息
        roomDetailVo.setGraphVoList(graphVoList);
        // 设置基本属性信息
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        // 设置配套信息
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        // 设置标签信息
        roomDetailVo.setLabelInfoList(labelInfoList);
        // 设置支付方式信息
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        // 设置杂费信息
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        // 设置租期信息
        roomDetailVo.setLeaseTermList(leaseTermList);

        // 浏览记录
        browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(), id);

        // 返回封装好的房间详细信息对象
        return roomDetailVo;

    }

    /**
     * 根据公寓id分页查询房间列表
     * @param page
     * @param id
     * @return
     */
    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.pageItemByApartmentId(page, id);
    }
}




